home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: Floating point calculation order
- Date: Mon, 29 Jan 96 23:11:58 GMT
- Organization: none
- Message-ID: <822957118snz@genesis.demon.co.uk>
- References: <m0tedv8-0002eqC@sice.nsk.su> <3104c6d9.134061184@nntp.ix.netcom.com> <DLnE5K.2xH@microunity.com> <822428038snz@genesis.demon.co.uk> <4e8dg1$ehg@ns.RezoNet.NET>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4e8dg1$ehg@ns.RezoNet.NET> ray@ultimate-tech.com "Ray Dunn" writes:
-
- >In referenced article, Lawrence Kirby says...
- >>Parentheses in C allow you to change the operator/operand grouping
- >>within expressions. They don't enforce any order of evaluation beyond
- >>that implied by the grouping. So:
- >>
- >> x = p * q / r;
- >>
- >>and
- >>
- >> x = (p * q) / r;
- >>
- >>mean *exactly* the same thing in C since the parentheses don't change
- >>the grouping.
- >
- >You're right, of course, but they only mean the same thing because *
- >and / have the same precedence and associate left to right.
- >
- >Remember that the original posting was from someone complaining that
- >the division was done first (presumably on a non-conforming compiler).
- >In this case parenthesizing the multiplication would indeed change the
- >order of evaluation and solve his problem.
-
- There are two possibilities here:
-
- 1. The compiler got the precedence/associativity rules wrong and the
- parentheses would make a difference.
-
- 2. The compiler got the precedence/associativity right but decided to
- reorder as an 'optimisation' based on the mathematical equivalence of
- the 2 orderings. Here the parentheses are unlikely to make a difference.
-
- I think 2 is by far the more likely possibility.
-
- >I do think it's a good idea to parenthesize expressions to make it
- >clear what you expect to happen.
-
- Agreed, although excessive parenthesization can be as bad (worse?) than too
- little.
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-